package com.zx.mes.rocketmq.hyl.transaction;

import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl;
import org.apache.rocketmq.client.producer.LocalTransactionState;
import org.apache.rocketmq.client.producer.TransactionListener;
import org.apache.rocketmq.common.message.Message;
import org.apache.rocketmq.common.message.MessageExt;

/**
 * 添加事务监听
 *
 * @author 华云龙
 * @date 2018-12-3
 */
@Log4j2
public class TransactionListenerImpl implements TransactionListener {

    @Override
    public LocalTransactionState executeLocalTransaction(Message msg, Object arg) {
        log.info("执行本地事务单元...");
        log.info("传过来的消息:" + msg + ",参数:" + arg);
//        return LocalTransactionState.COMMIT_MESSAGE;
        return LocalTransactionState.UNKNOW;
    }

    /**
     * 回检的过程下面的类中进行
     * org.apache.rocketmq.client.impl.producer.DefaultMQProducerImpl.checkTransactionState()
     * 具体的调用步骤在下面这个代码里
     * localTransactionState = transactionListener.checkLocalTransaction(message);
     *
     * @param msg
     * @return
     */
    @Override
    public LocalTransactionState checkLocalTransaction(MessageExt msg) {
///        Integer status = Integer.valueOf(msg.getTransactionId());
        log.info("--------回检状态,check transaction------:" + msg);
///        log.info("状态值:"+status);

        return LocalTransactionState.COMMIT_MESSAGE;
    }
}
